﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.NetClasses;
namespace GameServer
{
    class Server : Engine.NetClasses.BaseServer
    {
        private Dictionary<PacketType, Action<PacketType, PacketsBilder , IAsyncClient>> PacetAction;
        public Server()
            : base()
        {
            PacetAction = new Dictionary<PacketType, Action<PacketType, PacketsBilder , IAsyncClient>>(0);

            PacetAction.Add(PacketType.RequestGame, new Action<PacketType, PacketsBilder, IAsyncClient>(Actions.ReqestGame));
        }
        public override void Update()
        {

            base.Update();
        }
        public override void ReadData(Engine.NetClasses.IAsyncClient client)
        {
            PacketsBilder[] pacets = client.GetFromClient();
            for (int i = 0; i < pacets.Length; i++)
            {
                PacketType type = (PacketType)Enum.Parse(typeof(PacketType), pacets[i].Header._ptype.ToString(), true);
                doAction(type, pacets[i], client);

            }
            base.ReadData(client);
        }

        private void doAction(PacketType _type, PacketsBilder pacets, IAsyncClient client)
        {
            if (PacetAction.ContainsKey(_type))
            {
                PacetAction[_type].Invoke(_type, pacets, client); 
            }         
        }
    }
}
